import axios from "axios"
import Cookies from "js-cookie"
// import NProgress from "`nprogress`"
import {
  Message
} from "element-ui"
// axios默认配置
axios.defaults.timeout = 50000 // 超时时间
axios.defaults.baseURL = process.env.API_HOST

// http request 拦截器
axios.interceptors.request.use(config => {
  config.headers["Content-Type"] = "application/json;charset=UTF-8"
  // config.headers["Content-Type"] = "application/x-www-form-urlencoded;charset=UTF-8"

  // config.headers.Authorization = 'bearer' + localStorage.getItem('token')
  // console.log(localStorage.getItem('token'))
  return config
},
  error => {
    return Promise.reject(error.response)
  })

// http response 拦截器
axios.interceptors.response.use(response => {
  if (response.data.statusCode == 200) {
    return Promise.resolve(response)
  } else {
    Message({
      type: 'warning',
      message: response.data.content
    })
    console.log('-----', response.data.statusCode)
    return Promise.resolve()
  }
},
  error => {
    console.log(error)
    if (error.response.status === 404) {
      Message({
        message: "请求地址出错",
        type: "warning"
      })
    } else if (error.response.status === 401) { //TOKEN过期重新请求
      console.log(localStorage.getItem('token'))
      Message({
        message: "登陆状态过期，请重新登陆",
        type: "warning"
      })
      localStorage.clear();
      setTimeout(() => {
        window.location.href = 'http://192.168.3.46:8081'
      }, 1200)
    } else if (error.response.status === 500) {
      Message({
        message: "服务异常,请联系管理员-code: 500",
        type: "error"
      })
    } else if (error.response.status === 504) {
      Message({
        message: "网关超时,请联系管理员",
        type: "error"
      })
    } else {
      Message({
        message: "系统未知错误,请联系管理员",
        type: "error"
      })
    }
    return Promise.reject(error.response) // 返回接口返回的错误信息
  })
export default axios